<template>
  <div>
    <template>
      <Card style="width:850px;margin: auto;">
        <Form :model="formLeft" label-position="left" :rules="validateLibForm" :label-width="100" style="width: 300px;margin: auto;">

          <FormItem label="原料代码:" prop="code">
            <Input v-model="formLeft.code" :maxlength="10"></Input>
          </FormItem>

          <FormItem label="原料名称:" prop="name">
            <Input v-model="formLeft.name" :maxlength="10"></Input>
          </FormItem>

          <FormItem label="描述:" prop="description">
            <Input v-model="formLeft.description" :maxlength="100" type="textarea" style="width: 200px"/>
          </FormItem>

          <FormItem label="重量:"  prop="weight">
            <Input v-model="formLeft.weight" :maxlength="10"></Input>
          </FormItem>

          <FormItem label="单位:"  prop="unit">
            <Input v-model="formLeft.unit" :maxlength="10"></Input>
          </FormItem>

          <row type="flex" justify="center">
            <i-col span="20">

            </i-col>
            <i-col span="30">
              <Button type="primary" @click="MaterialInsert">添加</Button>
              <Button type="success" style="margin-left: 160px" @click="back">返回</Button>
            </i-col>
          </row>

        </Form>
      </Card>
    </template>
  </div>
</template>

<script>
  import {MaterialInsert} from "../../../api/data"

  export default {
    data() {
      return {
        formLeft: {
          code: '',
          description: '',
          name: '',
          weight: '',
          unit: ''
        },
        validateLibForm: {
          code: [
            {required: true, message: '请输入', trigger: 'blur'}
          ],
          description: [
            {required: true, message: '请输入', trigger: 'blur'}
          ],
          name: [
            {required: true, message: '请输入', trigger: 'blur'}
          ],
          weight: [
            {required: true, message: '请输入', trigger: 'blur'}
          ],
          unit: [
            {required: true, message: '请输入', trigger: 'blur'}
          ],
        }
      }
    },
    mounted() {

    },
    methods: {
      MaterialInsert() {
        if (this.formLeft.code !== null && this.formLeft.code !== "") {
          if (this.formLeft.name !== null && this.formLeft.name !== "") {
            if (this.formLeft.description !== null && this.formLeft.description !== "") {
              if (this.formLeft.weight !== null && this.formLeft.weight !== "") {
                if (this.formLeft.unit !== null && this.formLeft.unit !== "") {
                  MaterialInsert(this.formLeft).then(res => {
                    this.$Message.success("添加成功");
                    this.$router.go(-1)
                  })
                }else {
                  this.$Message.error("单位不能为空");
                }
              } else {
                this.$Message.error("重量不能为空");
              }
            } else {
              this.$Message.error("描述不能为空");
            }
          } else {
            this.$Message.error("原料名称不能为空");
          }
        } else {
          this.$Message.error("原料代码不能为空");
        }
      },
      back() {
        this.$router.go(-1);
      },

    }
  }
</script>



